#ifndef ELECTRONICSCALEINTERVALRATE_H
#define ELECTRONICSCALEINTERVALRATE_H

#include <QObject>
#include <QTimer>
#include "moduleelectronicscaledefine.h"

class ElectronicScaleIntervalRate : public QObject
{
    Q_OBJECT
public:
    explicit ElectronicScaleIntervalRate(QObject *parent = nullptr);
    ~ElectronicScaleIntervalRate();

    void setInterval(quint32 second);
    quint32 getInterval() const;

    bool exportIntervalRateToXlsx(const QString &path);
    bool importIntervalRateFromXlsx(const QString &path);

    electronicscale::IntervalRate getIntervalRate() const;
    bool setIntervalRates(QList<electronicscale::IntervalRate> &rates);
    QList<electronicscale::IntervalRate> getIntervalRates() const;

    void addSample(electronicscale::Sample sample);

    void start();
    void stop();

    bool isStart() const;
    bool isCalculate() const;
    electronicscale::IntervalRate calculate();
    electronicscale::IntervalRate stopToCalculate();

    // 计算速度，也用于生成上升曲线
    static QList<electronicscale::IntervalRate> calculate(const QList<electronicscale::Sample> &samples, quint32 interval);

signals:

public slots:
    void onTimeout();

private:
    quint32                                 m_interval = 60; // s
    quint32                                 m_intervalCnt;

    electronicscale::Sample                 m_prevSample;
    electronicscale::IntervalRate           m_rate; // 瞬时速度 g/h
    QList<electronicscale::IntervalRate>    m_rates;

    bool                                    m_isCalculate = false;
    bool                                    m_isStart = false;
    bool                                    m_isGetFirstData = false;

    QList<electronicscale::Sample>          m_samples;
};

#endif // ELECTRONICSCALEINTERVALRATE_H
